package cn.com.huzo.webapp.controller.admin;

import cn.com.huzo.common.util.ApplicationConfiguration;
import huzofw.common.util.annotations.WriteLog;
import huzofw.common.web.controller.BaseFormController;
import huzofw.model.entities.identity.IdentityUserEntity;
import huzofw.model.entities.identity.AccountViewEntity;
import huzofw.model.entities.identity.ResetPasswordViewEntity;
import huzofw.service.security.IdentityUserManager;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 系统成员控制器
 *
 * @author 吴旭
 */
@Controller
public class IdentityUserController extends BaseFormController {

    // <editor-fold defaultstate="collapsed" desc="属性">
    private final String rootNodeText = ApplicationConfiguration.getCompanyName(); //"大连华纳制衣有限公司"; 
    @Resource(name = "IdentityUserManager")
    private IdentityUserManager _manager;

    @Resource(name = "shiroEhcacheManager")
    EhCacheManager cacheManager;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="画面处理部分">
    /**
     * 跳转到系统成员一览画面
     * <p>
     * @return
     */
    @RequestMapping("admin/membership/list.html")
    public ModelAndView redirectListWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("admin.membership.list.def");
        return view;
    }

    /**
     * 弹出系统成员选择画面
     * <p>
     * @return
     */
    @RequestMapping("admin/membership/choice.html")
    public ModelAndView redirectMembershipChoiceWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("popup.admin.membership.choice.def");
        return view;
    }
    
    /**
     * 转到更改密码画面
     * <p>
     * @return
     */
    @RequestMapping("admin/password/change.html")
    public ModelAndView redirectUserChangePasswordWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("admin.password.change.def");
        return view;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="数据处理部分">
    /**
     * 根据条件取得系统成员分页数据。
     * <p>
     * @param filters 检索条件
     * @return 系统成员分页数据
     * @throws Exception
     */
    @RequestMapping(value = "admin/membership/fetchForPage.html",
        method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchForPage(@RequestBody Map<String, Object> filters) throws Exception {
        filters.put("RootNodeText", rootNodeText);
        return _manager.fetchForPage(filters);
    }

    /**
     * 批量增加系统成员。
     *
     * @param itemList 要处理的数据列表
     * @return
     *
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "admin/membership/insert.html",
        method = RequestMethod.POST)
    @WriteLog(modelName = "系统管理→系统用户", typeOfOperated = "增加")
    public @ResponseBody
    Integer insert(@RequestBody final List<IdentityUserEntity> itemList) throws Exception {
        int rowsAffected = 0;
        
        if (itemList.size() > 0) {
            rowsAffected = _manager.insert(itemList);
        }

        return rowsAffected;
    }

    /**
     * 删除系统成员。
     *
     * @param instance 要删除的成员用户标识
     * @return 影响行数
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "admin/membership/delete.html",
        method = RequestMethod.POST)
    public @ResponseBody
    Integer delete(@RequestBody final AccountViewEntity instance) throws Exception {
        return _manager.delete(instance.getUsername());
    }

    /**
     * 改变密码。
     *
     * @param instance 改变密码信息
     * @return 成功标记。
     * @throws java.lang.Exception
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "admin/password/change.html",
        method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> changePassword(@RequestBody final ResetPasswordViewEntity instance) throws Exception {
        Map<String, Object> result = new HashMap();
        result.put("success", _manager.changePassword(instance.getUsername(), instance.getOldPassword(), instance.getNewPassword()));
        return result;
    }
    // </editor-fold>
}
